package com.djk.daily.thread.stopthreads;

/**
 * @author dujunkai
 * @version 1.0
 * @description: 带有sleep的中断线程的写法
 * run方法中判断中断没有意义，中断通知的时候循环已经执行结束，开始阻塞1秒
 * @date 2021/7/2 14:14
 */
public class RightWayStopThreadWithSleep {
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            int num = 0;
            System.out.println("系统时间：" + System.currentTimeMillis() + "  " + Thread.currentThread().getName() + "开始执行");
            try {
                while (!Thread.currentThread().isInterrupted() && num <= 300000000) {
                    if (num % 100000 == 0) {
//                        System.out.println("系统时间：" + System.currentTimeMillis() + "  " + num + "是100的倍数");
                    }
                    num++;
                }
                System.out.println("系统时间：" + System.currentTimeMillis() + "  " + (num - 1) + "是100的倍数");
                System.out.println("系统时间：" + System.currentTimeMillis() + "  " + "执行完毕时间");
                System.out.println("系统时间：" + System.currentTimeMillis() + "  " + Thread.currentThread().getName() + "开始阻塞1秒");
                Thread.sleep(1000);
                System.out.println("系统时间：" + System.currentTimeMillis() + "  " + Thread.currentThread().getName() + "阻塞1秒结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
        System.out.println("系统时间：" + System.currentTimeMillis() + "  " + Thread.currentThread().getName() + "开始阻塞0.5秒");
        Thread.sleep(500);
        System.out.println("系统时间：" + System.currentTimeMillis() + "  " + thread.getName() + "开始中断");
        thread.interrupt();
    }
}
